package uk.ac.manchester.tewpl.iceiface.client.explorer.ui;

import java.util.Collection;

import uk.ac.manchester.tewpl.iceiface.client.Functors.ParamVoidCallback;
import uk.ac.manchester.tewpl.iceiface.client.Functors.TypedCallback;
import uk.ac.manchester.tewpl.iceiface.client.explorer.Utils;
import uk.ac.manchester.tewpl.iceiface.client.explorer.nodes.Airport;
import uk.ac.manchester.tewpl.iceiface.client.explorer.nodes.Terminal;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.MarginData;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;

public class AirportPanel extends LayoutContainer {
	
	Utils utils = Registry.get("utils");
	
	// Local variables
	private Airport airport;
	private int selectionNumber = 0;
	
	// Panel object variables
	private MarginData marginData = new MarginData(6,10,0,6); // Margin data
	
	// Panels
	private LayoutContainerMouse terminalsContainer = new LayoutContainerMouse();
	private LayoutContainerMouse filterDetailsContainer  = new LayoutContainerMouse();
	
	private Boolean showTerminals = false;
	
	/*
	 * Constructor
	 */
	public AirportPanel(Airport a, int num) {
		super();
		airport = a; // Store relevant airport instance
		setStyleName("airport-panel"); // Set CSS style
		selectionNumber = num;
		
		// Add detail containers
		add(createNameContainer(), marginData);
		add(createTerminalsHeaderContainer(), marginData);
		add(terminalsContainer);
		add(filterDetailsContainer);
		add(createAddFilterContainer(), marginData);
		
		// Set CSS
		terminalsContainer.setStyleName("terminals-container");
		
		// Populate containers
		updateTerminalsContainer();
		updateFilterDetailsContainer();
	}
	
	/*
	 * Widget rendering methods
	 */
	public void updatePanel() {
		// Call section methods
		updateTerminalsContainer();
		updateFilterDetailsContainer();
		
		// Update layout
		layout();
	}
	
	private LayoutContainer createNameContainer() {
		// Create container
		LayoutContainer nameContainer = new LayoutContainer();
		
		nameContainer.setStyleName("airport-name");
		nameContainer.add(new Text(selectionNumber + ". " + airport.getLabel()));
		
		return nameContainer;
	}
	
	private LayoutContainerMouse createTerminalsHeaderContainer() {
		// Create container
		final LayoutContainerMouse terminalsHeaderContainer = new LayoutContainerMouse();
		
		// CSS variables
		final String baseStyle = "panel-line-format terminals-header";
		final String offStyle = " expand-icon";
		final String onStyle = " retract-icon";
		
		if (airport.getTerminals().size() == 0) {
			terminalsHeaderContainer.setStyleName(baseStyle);
			terminalsHeaderContainer.add(new Text("(No terminals)"));
		} else {
			terminalsHeaderContainer.setStyleName(baseStyle + offStyle);
			
			// Add the filter detail
			terminalsHeaderContainer.add(new Text("Terminals"));
			
			// Add mouse handlers
			//terminalsHeaderContainer.addMouseHoverStyler(baseStyle, "");
			terminalsHeaderContainer.addMouseDownHandler(new MouseDownHandler() {
				@Override
				public void onMouseDown(MouseDownEvent arg0) {
					if (showTerminals) {
						terminalsContainer.setStyleAttribute("display", "none");
						terminalsHeaderContainer.setStyleName(baseStyle + offStyle);
					} else {
						terminalsContainer.setStyleAttribute("display", "block");
						terminalsHeaderContainer.setStyleName(baseStyle + onStyle);
					}
					
					showTerminals = !showTerminals;
				}
			});
		}
		
		return terminalsHeaderContainer;
		
	}
	
	private void updateTerminalsContainer() {
		// Clear terminals container
		terminalsContainer.removeAll();
		// Add each terminal, with result count
		for (final Terminal terminal : airport.getTerminals()) {
			// Container for the filter detail
			LayoutContainerMouse terminalContainer = new LayoutContainerMouse();
			String baseStyle = "panel-line-format terminal-detail";
			terminalContainer.setStyleName(baseStyle);
			
			// Add the filter detail
			terminalContainer.add(new Text(terminal.getLabel() + " (" + terminal.getCount() + ")"));
			
			// Add mouse handlers
			/*terminalContainer.addMouseHoverStyler(baseStyle, "terminal-detail-hover");
			terminalContainer.addMouseDownHandler(new MouseDownHandler() {
				@Override
				public void onMouseDown(MouseDownEvent arg0) {
					Info.display("", "Clicked: " + terminal.getLabel());
				}
			});*/
			
			// Add filter details to filter container
			terminalsContainer.add(terminalContainer, new MarginData(6,10,0,3));
		} // for
	}
	
	private void updateFilterDetailsContainer() {
		filterDetailsContainer.removeAll();
		Collection<String> filterKeywords = airport.getFilterKeywords();
		if (filterKeywords != null) {			
			// Add each terminal, with result count
			for (String keyword : filterKeywords) {
				// Container for the filter detail
				LayoutContainerMouse filterDetailContainer = new LayoutContainerMouse();
				String baseStyle = "panel-line-format filter-detail";
				filterDetailContainer.setStyleName(baseStyle);
				filterDetailContainer.addMouseHoverStyler(baseStyle, "filter-detail-hover"); // Hover style helper
				
				// Add the filter detail
				filterDetailContainer.add(new Text(keyword));
				
				// Add filter details to filter container
				filterDetailsContainer.add(filterDetailContainer, new MarginData(6,10,0,3));
			} // for
		} // if
	}
	
	private LayoutContainerMouse createAddFilterContainer() {
		// Create container
		LayoutContainerMouse addFilterContainer = new LayoutContainerMouse();
		String baseStyle = "panel-line-format filter-add";
		addFilterContainer.setStyleName(baseStyle);
		
		// Add the filter detail
		addFilterContainer.add(new Text("Add new filter"));
		
		// Add mouse handlers
		addFilterContainer.addMouseHoverStyler(baseStyle, "filter-add-hover"); // Hover style helper
		addFilterContainer.addMouseDownHandler(new MouseDownHandler() {
			@Override
			public void onMouseDown(MouseDownEvent arg0) {
				utils.promptAddKeywordFilter(airport);
			}
		});
		
		return addFilterContainer;
	}
	
	public Airport getAirport() {
		return airport;
	}
	
	/*
	 * Check for airport equality
	 */
	@Override
	public boolean equals(Object o) {
		if (o == null)
			return false;
		else if (o.getClass() == AirportPanel.class)
			return airport.equals(((AirportPanel) o).getAirport());
		else if (o.getClass() == Airport.class)
			return airport.equals(((Airport)o));
		else
			return this == o; 
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return airport.hashCode();
	}

}
